package mosync.mobilelualiveditor.ui;

import java.util.ArrayList;
import java.util.Stack;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

class WidgetParser extends DefaultHandler
{
	public WidgetParser()
	{
		mWidgetStack.clear();
		mScreens.clear();
	}

	public void startElement(String namespaceURI, String localName,
		String qName, Attributes atts)
	{
		if (localName.equals("Widget"))
		{
			WidgetInstance w = new WidgetInstance(WidgetType.getTypeByName(atts
				.getValue("type")), atts.getValue("name"));
			if (mWidgetStack.size() != 0)
				mWidgetStack.peek().addChild(w);
			mWidgetStack.push(w);
		}
		else if (localName.equals("Property"))
		{
			mWidgetStack.peek().setProperty(atts.getValue("name"),
				atts.getValue("value"));
		}
	}

	public void endElement(String namespaceURI, String localName, String qName)
	{
		if (localName == "Widget")
		{
			WidgetInstance root = mWidgetStack.pop();
			if (mWidgetStack.isEmpty())
			{
				// This means we have a screen...
				mScreens.add(root);
			}
		}
	}

	ArrayList<WidgetInstance> getScreens()
	{
		return mScreens;
	}

	private ArrayList<WidgetInstance> mScreens = new ArrayList<WidgetInstance>();
	private Stack<WidgetInstance> mWidgetStack = new Stack<WidgetInstance>();
	// private WidgetInstance root = null;
}
